package com.example.testclient;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.example.testclient.glideSetting.GlideEngine;
import com.example.testclient.netty.NettyClient;
import com.example.testclient.eventBusBean.ConnectStatusBean;
import com.haoxy.common.model.FileBean;
import com.haoxy.common.model.MessageBean;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.luck.picture.lib.tools.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    Button btSendMsg, btSendFile, btStart, btStop;
    ImageView img, imgStatus;
    public static final String ADRESS = "192.168.2.106";
//    public static final String ADRESS = "192.168.1.100";//测试时将地址改成服务端WiFi的ip地址
    public static final int PORT = 10010;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        img = findViewById(R.id.img);
        imgStatus = findViewById(R.id.img_status);
        btStart = findViewById(R.id.bt_start);
        btStop = findViewById(R.id.bt_stop);
        btSendMsg = findViewById(R.id.bt_send_msg);
        btSendFile = findViewById(R.id.bt_send_file);

        btStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NettyClient.getInstance().stop();
                NettyClient.getInstance().start(ADRESS, PORT);
            }
        });
        btStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NettyClient.getInstance().stop();
            }
        });
        btSendMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NettyClient instance = NettyClient.getInstance();
                if (null == instance.channel || !instance.channel.isOpen()) {
                    ToastUtils.s(MainActivity.this, "请先连接服务端");
                    return;
                }
                MessageBean messageBean = new MessageBean();
                messageBean.setMsg("this is 测试数据");
                instance.sendMessage(messageBean);
            }
        });

        btSendFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NettyClient instance = NettyClient.getInstance();
                if (null == instance.channel || !instance.channel.isOpen()) {
                    ToastUtils.s(MainActivity.this, "请先连接服务端");
                    return;
                }
                openPic();
            }
        });
    }

    private void openPic() {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .maxSelectNum(1)
                .minSelectNum(1)
                .isMaxSelectEnabledMask(true)
                .selectionMode(PictureConfig.SINGLE)
                .loadImageEngine(GlideEngine.createGlideEngine())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> result) {
                        File file = new File(result.get(0).getPath());
                        FileBean fileBean = new FileBean();
                        fileBean.setBytes(FileUtils.File2byte(file));
                        fileBean.setFileName(file.getName());
                        Glide.with(MainActivity.this).load(file).into(img);
                        NettyClient.getInstance().sendFile(fileBean);
                    }

                    @Override
                    public void onCancel() {
                        // 取消
                    }
                });
    }

    /**
     * eventbus事件处理
     *
     * @param o
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRecevie(Object o) {
        if (null != o) {
            if (o instanceof MessageBean) {
                MessageBean messageBean = (MessageBean) o;
                Toast.makeText(MainActivity.this, messageBean.getMsg(), Toast.LENGTH_SHORT).show();
            } else if (o instanceof ConnectStatusBean) {
                ConnectStatusBean connectStatusBean = (ConnectStatusBean) o;
                if ("1".equals(connectStatusBean.getStatus())) {
                    Glide.with(MainActivity.this).load(ContextCompat.getDrawable(MainActivity.this, R.mipmap.show_wifi)).into(imgStatus);
                } else {
                    Glide.with(MainActivity.this).load(ContextCompat.getDrawable(MainActivity.this, R.mipmap.no_wifi)).into(imgStatus);
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        NettyClient.getInstance().stop();
    }
}